package chapter06

/**
 * Created with IntelliJ IDEA.
 * ClassName: Test12_Singleton
 * Package: chapter06
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 *          Date: 2023-06-22
 *          Time: 20:59
 */

//使用伴生对象 实现单例模式
object Test12_Singleton {
  def main(args: Array[String]): Unit = {
    val student1 = Student12.getInstance();
    student1.printInfo()
    val student2 = Student12.getInstance()
    student2.printInfo()

    println(student1)
    println(student2)
  }
}

class Student12 private(val name: String, val age: Int) {
  def printInfo() {
    //所有的访问都是基于对象的 伴生对象也是如此
    println(s"student:name = ${name},age = ${age},school = ${Student11.school}")
  }
}
////饿汉模式
//object Student12{
//  private val student:Student12 = new Student12("alice",12)
//  def getInstance(): Student12 = student
//}
object Student12 {
  private var student: Student12 = _

  def getInstance(): Student12 = {
    if (student == null) {
      return new Student12("alice", 20)
    }
    student
  }

}